package com.lishe.lishe.lishe.module.mall.presenter;

import android.text.TextUtils;

import com.lishe.lishe.lishe.module.mall.bean.category.CategoryFirst;
import com.lishe.lishe.lishe.module.mall.bean.category.CategoryGoods;
import com.lishe.lishe.lishe.module.mall.interfaces.ClassificationService;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.lishe.lishe.retrofit.bean.DataPageItemBody;


import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yang on 2017/12/19.
 */

public class ClassificationPresenter {
    private static ClassificationPresenter presenter;

    public static ClassificationPresenter getInstance() {
        if (presenter == null) {
            presenter = new ClassificationPresenter();
        }
        return presenter;
    }


    /**
     * 商城模块-获取一级分类
     *
     * @param listener
     */
    public void categoryFirstList(final DataResponseListener<List<CategoryFirst>> listener) throws Exception {

        RetrofitFactory.create().create(ClassificationService.class)
                .categoryFirstList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<List<CategoryFirst>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<List<CategoryFirst>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    /**
     * 商城模块-获取二级分类
     *
     * @param listener
     */
    private Disposable sendDisposable;

    public void categorySecondList(String cate_id, final DataResponseListener<List<CategoryFirst>> listener) throws Exception {
        if (sendDisposable != null) {
            sendDisposable.dispose();
        }

        RetrofitFactory.create().create(ClassificationService.class)
                .categorySecondList(cate_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<List<CategoryFirst>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        sendDisposable = d;
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<List<CategoryFirst>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 商城模块-获取更多分类
     *
     * @param listener
     */
    public void getMoreItems(String activity_config_id, String bind, String price, String sold, String shop, String priceRange,
                             int page, int pageSize,final DataResponseListener<DataPageItemBody<CategoryGoods>> listener) throws Exception {

        if (TextUtils.isEmpty(bind)) {
           bind=null;
        }
        if (TextUtils.isEmpty(price)) {
            price=null;
        }
        if (TextUtils.isEmpty(sold)) {
            sold=null;
        }
        if (TextUtils.isEmpty(shop)) {
            shop=null;
        }
        if (TextUtils.isEmpty(priceRange)||priceRange.equals("0,0")) {
            priceRange=null;
        }

        RetrofitFactory.create().create(ClassificationService.class)
                .getMoreItems(activity_config_id, bind, price, sold, shop, priceRange, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<DataPageItemBody<CategoryGoods>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<DataPageItemBody<CategoryGoods>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 商城模块-获取更多分类
     *
     * @param listener
     */
    public void categoryItems(String cate_id, String original_cate_id, String bind, String price, String sold, String shop,
                              String priceRange, int page, int pageSize,final DataResponseListener<DataPageItemBody<CategoryGoods>> listener) throws Exception {

        if (TextUtils.isEmpty(bind)) {
           bind=null;
        }
        if (TextUtils.isEmpty(price)) {
            price=null;
        }
        if (TextUtils.isEmpty(sold)) {
            sold=null;
        }
        if (TextUtils.isEmpty(shop)) {
            shop=null;
        }
        if (TextUtils.isEmpty(priceRange)||priceRange.equals("0,0")) {
            priceRange=null;
        }


        RetrofitFactory.create().create(ClassificationService.class)
                .categoryItems(cate_id, original_cate_id, bind, price, sold, shop, priceRange, page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestBody<DataPageItemBody<CategoryGoods>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        listener.onPrepare();
                    }

                    @Override
                    public void onNext(@NonNull BaseRequestBody<DataPageItemBody<CategoryGoods>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
